<?php
    class Admincp_Model_Contact extends Zend_Db_Table_Abstract
    {
        protected $_db;
        
        public function __construct()
        {
            $this->_db = Zend_Registry::get('db');
        }
        
        public function listContact($locale)
        {
            $sql = $this->_db->select()
                            ->from('contact AS c', array('id', 'status', 'phone'))
                            ->join('contact_language AS cl', 'c.id = cl.contact_id', array('name'))
                            ->join('language AS l', 'cl.language_id = l.id', null)
                            ->where('l.locale = ?', $locale)
                            ->order('c.id DESC');
            return $sql;
        }
        
        public function getContactById($id)
        {
            $sql = $this->_db->select()
                            ->from('contact')
                            ->where('id = ?', $id);
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function getContactLanguageById($id)
        {
            $sql = $this->_db->select()
                            ->from('contact_language AS cl', array('name'))
                            ->join('language AS l', 'cl.language_id = l.id', array('locale'))
                            ->where('cl.contact_id = ?', $id);
            $result = $this->_db->fetchAll($sql);
            return $result;
        }
        
        public function checkContact($name, $id = null)
        {
            $sql = $this->_db->select()
                            ->from('contact_language')
                            ->where('name = ?', $name);
            if($id != null) {
                $sql = $sql->where('contact_id <> ?', $id);
            }
            $result = $this->_db->fetchAll($sql);
            return count($result);
        }
        
        public function addContact($data)
        {
            $this->_db->insert('contact',$data);
            return $this->_db->lastInsertId();
        }
        
        public function addContactLanguage($data)
        {
            $this->_db->insert('contact_language',$data);
        }
        
        public function updateContact($data, $id)
        {
            $where = $this->getAdapter()->quoteInto('id = ?', $id);
            $this->_db->update('contact', $data, $where);
        }
        
        public function updateContactLanguage($data,$id)
        {
            $where = 'contact_id = '.$id.' AND language_id = '.$data['language_id'];
            $this->_db->update('contact_language', $data, $where);
        }
        
        public function deleteContact($id)
        {
            $where = $this->getAdapter()->quoteInto('id = ?', $id);
            $this->_db->delete('contact', $where);
            $where = $this->getAdapter()->quoteInto('contact_id = ?', $id);
            $this->_db->delete('contact_language', $where);
        }
        
        public function deleteMultiContact($listId)
        {
            foreach($listId as $id) {
                $this->deleteContact($id);
            }
        }
    }
